package com.yanhui.api.image;

import com.yanhui.modal.image.ocr.Youtu;
import com.yanhui.utils.ApiAccount;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.io.File;

/**
 * ocr识别
 * 2018-11-28 3种
 * 1、通用优图 暂时免费
 * 2、手写优图 暂时免费
 * 3、test4j验证码识别
 *
 * https://www.microsoft.com/zh-cn/download/details.aspx?id=40784
 */
public class OcrApi {

    public static void main(String[] args) throws TesseractException {

    }

    /**
     * 通用ocr
     * @param url
     * @return
     * @throws Exception
     */
    public String ocrGeneral(String url) throws Exception {
        String result = "";
        Youtu faceYoutu =
                new Youtu(
                        ApiAccount.YOUTU_APP_ID, ApiAccount.YOUTU_SECRET_ID,
                        ApiAccount.YOUTU_SECRET_KEY, Youtu.API_YOUTU_END_POINT,
                        ApiAccount.YOUTU_USER_ID);
        org.json.JSONObject response = faceYoutu.GeneralOcrUrl(url); //通用印刷体文字识别
        String alist = response.get("items").toString();
        org.json.JSONArray array = new org.json.JSONArray(alist);
        //JSONArray遍历
        for(int i=0; i<array.length(); i++) {
            org.json.JSONObject jsonObj = array.getJSONObject(i);
            result += jsonObj.get("itemstring").toString()+ "\r\n";
        }
        return result;
    }

    /**
     * 手写ocr
     * @param url
     * @return
     * @throws Exception
     */
    public String ocrHandwriting(String url) throws Exception {
        String result = "";
        Youtu faceYoutu =
                new Youtu(
                        ApiAccount.YOUTU_APP_ID, ApiAccount.YOUTU_SECRET_ID,
                        ApiAccount.YOUTU_SECRET_KEY, Youtu.API_YOUTU_END_POINT,
                        ApiAccount.YOUTU_USER_ID);
        org.json.JSONObject response = faceYoutu.HandwritingOcrUrl(url); //手写OCR文字识别
        String alist = response.get("items").toString();
        org.json.JSONArray array = new org.json.JSONArray(alist);
        //JSONArray遍历
        for(int i=0; i<array.length(); i++) {
            org.json.JSONObject jsonObj = array.getJSONObject(i);
            result += jsonObj.get("itemstring").toString()+ "\r\n";
        }
        return result;
    }



    /**
     * ocr识别验证码
     * @param fileName
     * @return
     * @throws TesseractException
     */
    public static String ocrCode(String fileName) throws TesseractException {
        ITesseract instance = new Tesseract();
        //如果未将tessdata放在根目录下需要指定绝对路径
        //设置训练库的位置
        instance.setDatapath("D:\\IdeaProjects\\yanhui-sdk");
        //instance.setDatapath("the absolute path of tessdata");

        //如果需要识别英文之外的语种，需要指定识别语种，并且需要将对应的语言包放进项目中
        //根据需求选择语言库
        // chi_sim ：简体中文
        // eng ：英文
        instance.setLanguage("eng");
        // instance.setLanguage("osd");
        // instance.setLanguage("chi_sim2");

        // 指定识别图片
        File imgDir = new File(fileName);
        long startTime = System.currentTimeMillis();
        String ocrResult = instance.doOCR(imgDir);

        // 输出识别结果
        System.out.println("识别结果: \n" + ocrResult + "\n 耗时：" + (System.currentTimeMillis() - startTime) + "ms");

        return ocrResult;
    }

}